<template>
	<view class="container">
		<u-navbar :title="Data.title" placeholder :autoBack="true">
		</u-navbar>
		<!-- 选项 -->
		<view class="content">
			<view class="title">{{Data.title}}</view>
			<view class="desc">
				{{Data.describeVote}}
			</view>
			<!-- 选项 -->
			<view class="option">
				<!-- item.id == isVote.id?'option-item-active':'option-item' -->
				<view class="option-item"
					:class="isVote == null ? 'option-item':(isVote.optioned == item.option ? 'option-item-active' : 'option-item' )"
					v-for="(item,index) in optionList" :key="index" @click="optionChange(item)">
					<view class="name">
						{{item.option}}
					</view>
					<view class="value">
						<view class="">
							{{item.voteNumber}}票
						</view>
						<view v-if="total == 0">
							0%
						</view>
						<view v-else>
							{{(item.voteNumber / total * 100).toFixed(1)}}%
						</view>
					</view>
					<view class="line">
						<view v-if="total == 0">
						</view>
						<view v-else class="line-active" :style="{width:(item.voteNumber / total *100).toFixed(1)+'%'}">

						</view>
					</view>
				</view>
			</view>
		</view>
		<u-modal :show="show" :showCancelButton="showCancelButton" :showConfirmButton="showConfirmButton" :content='content' @confirm="confirm"
			@cancel="cancel">
			<u-loading-icon v-if="loadingShow"></u-loading-icon>
		</u-modal>
	</view>
</template>

<script>
	import {
		getVoteDetails,
		getnewVote,
		getVoteListoption
	} from '@/api/voteApi/index.js'
	import {
		getDate
	} from '@/utils/getDate.js'
	export default {
		data() {
			return {
				showCancelButton: false, //是否显示取消
				showConfirmButton:true,//是否显示确定
				show: false, //对话框
				loadingShow: false, //加载动画
				content: '',
				Data: null,
				optionList: [],
				total: 0,
				params: {},
				isVote: null, //判断用户是否投过票
				openid: null //用户id
			}
		},
		onLoad(option) {
			let that = this
			that.Data = JSON.parse(decodeURIComponent(option.data))
			uni.getStorage({
				key: 'openid',
				success: function(res) {
					console.log('获取openid', res)
					that.openid = res.data.openid
				}
			})
		},
		onShow() {
			this.init(this.Data.voteId)
		},
		methods: {
			// 获取投票列表
			async init(voteId) {
				const result = await getVoteDetails(voteId)
				const params = {
					userId:this.openid,
					// userId:'onfDbt09zNk_WD95F0HMyABHW-4M',
					voteId:this.Data.voteId
				}
				const option = await getVoteListoption(params)
				this.isVote = option.data.result
				// console.log('选中的谁', option)
				result.data.result.forEach(item => {
					this.total += item.voteNumber
				})
				this.optionList = result.data.result

			},
			// 选择option
			optionChange(item) {
				this.params = {
					userId: this.openid,
					// userId:'onfDbt09zNk_WD95F0HMyABHW-4M',
					voteSonId: item.id,
					optioned: item.option,
					voteId:this.Data.voteId
				}
				// 根据状态判断是否已经参与过投票
				if (this.isVote != null) {
					this.show = true
					this.showCancelButton = false; //不显示取消按钮
					this.showConfirmButton = true;
					this.content = '您已经参与过投票了，请勿重复投票'
				} else {
					this.show = true;
					this.showCancelButton = true; //显示取消按钮
					this.showConfirmButton = true;
					this.content = `您确定选择将宝贵的一票，投给${item.option}吗`
				}
			},
			// 点击确定事件
			confirm() {
				if (this.isVote != null) {
					this.show = false
				} else {
					this.loadingShow = true;
					getnewVote(this.params).then(res => {
						console.log('liebiao',res)
						if (res.data.code === 200) {
							this.init(this.Data.voteId)
							this.show = false
							this.loadingShow = false;
						}else{
							this.content = res.data.code;
							this.showConfirmButton = false;//确定不显示
							this.showCancelButton = true;//取消显示
						}
					})
				}


			},
			// 点击取消
			cancel() {
				this.show = false
			}
		},
	}
</script>

<style scoped lang="scss">
	.container {
		height: 100vh;
		background-color: #efefef;
	}

	::v-deep.u-navbar {
		.u-icon__icon {
			font-weight: 600 !important;
			color: black !important;
		}

		.u-navbar__content__title {
			font-weight: 600 !important;
			color: black !important;
		}
	}

	.content {
		padding: 20rpx;

		>view {
			margin-bottom: 40rpx;
		}

		.title {
			font-size: 40rpx;
			font-weight: 600;
		}

		.desc {
			font-size: 24rpx;
		}

		// 选中的样式
		.option-item-active {
			.name {
				position: relative;
			}

			// background-color: rgba(124,252,0, 0.5) !important;
			.name:after {
				content: '';
				display: inline-block;
				background-image: url('../../../static/partyBuilding/vote/yes.png');
				background-size: cover;
				width: 30rpx;
				height: 30rpx;
			}
		}

		// 普通样式
		.option-item {
			background-color: white;
			margin-bottom: 20rpx;
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			box-shadow: 1px 1px 5px 2px #c3b5b5;
			position: relative;

			.value {
				width: 25%;
				display: flex;
				justify-content: space-between;

				:nth-child(1) {
					margin-right: 20rpx;
				}
			}

			.line {
				position: absolute;
				bottom: 0;
				left: 0;
				z-index: 1222;
				width: 100%;
				height: 8rpx;
				border-radius: 4rpx;
				overflow: hidden;

				.line-active {
					height: 100%;
					background-color: blue;
				}

			}
		}
	}
</style>